package com.sl.au.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.sl.au.entity.AfterSaleTicket;
import com.sl.au.entity.AfterSaleTicketPay;
import com.sl.au.entity.ContractInfo;
import com.sl.au.repository.AfterSaleTicketPayRepository;
import com.sl.au.repository.AfterSaleTicketRepository;
import com.sl.au.service.AfterSaleInHouseService;
import com.sl.au.service.ContractService;

@Service("AfterSaleInHouseService")
public class AfterSaleInHouseServiceImpl extends BaseServiceImpl implements AfterSaleInHouseService {
	@Autowired
	AfterSaleTicketRepository afterSaleTicketRepository;
	@Autowired
	AfterSaleTicketPayRepository afterSaleTicketPayRepository;
	
	@Autowired
	ContractService contractService;

	@Override
	public List<AfterSaleTicket> getInHouseTicket(int start, int length, String key,String isSubmit) {
		Page<AfterSaleTicket> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleTicketRepository.findInHouseTicket(key, isSubmit,pageable);
		return pages.getContent();
	}

	@Override
	public AfterSaleTicketPayRepository getTicketPayRepository() {
		// TODO Auto-generated method stub
		return this.afterSaleTicketPayRepository;
	}

	@Override
	public List<AfterSaleTicketPay> getPayDevice(int start, int length, String ticketId, String orderName, String dir) {
		Page<AfterSaleTicketPay> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleTicketPayRepository.findPayDevice(ticketId, pageable);
		return pages.getContent();
	}

	@Override
	public AfterSaleTicketRepository getAfterSaleTicketRepository() {

		return this.afterSaleTicketRepository;
	}

	@Override
	public List<AfterSaleTicket> getAfterSaleAccess(List<AfterSaleTicket> ticket) {
		for(AfterSaleTicket a:ticket){
			if(a!=null){
				String contractId=a.getContractId();
				ContractInfo c=contractService.getContractInfoRepository().findOne(contractId);
				String type=c.getContractType();
				a.setContractType(type);
			}
		}
		return null;
	}

	@Override
	public List<AfterSaleTicket> getMyAfterSaleAccess(int start, int length,String key, String userId, String isSubmit) {
		Page<AfterSaleTicket> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = afterSaleTicketRepository.findMyTicket(key, userId, isSubmit, pageable);
		return pages.getContent();
	}

	@Override
	public long getMyAfterSaleAccessCount(String key, String userId, String isSubmit) {
		long count=0;
		count=afterSaleTicketRepository.getMyTicketCount(key, userId, isSubmit);
		return count;
	}

}
